package com.ekuaitu.kuaitustaff.utils.ble;

import android.os.Build;

import com.ekuaitu.kuaitustaff.api.ParseApiStore;
import com.ekuaitu.kuaitustaff.api.UrlConstant;
import com.ekuaitu.kuaitustaff.application.MyApplication;
import com.ekuaitu.kuaitustaff.utils.ThreadPool;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class BleLogUtil {
    private ArrayList<Map<String, String>> logList = new ArrayList<>();

    private static final BleLogUtil ourInstance = new BleLogUtil();
    private String orderId;
    private String carId;
    private String operateType;
    private String carBoxType;
    private String carBleName;

    public static BleLogUtil getInstance() {
        return ourInstance;
    }

    private BleLogUtil() {
    }

    public void initLog(String carId, String orderId, String operateType, String carBoxType, String carBleName) {
        this.operateType = operateType;
        this.orderId = orderId;
        this.carId = carId;
        this.carBoxType = carBoxType;
        this.carBleName = carBleName;
    }


    public void recordLog(String sign, String stepName, String exception, String extra) {
        if (sign.equals("begin")) {
            logList.clear();
        }
        Map<String, String> logMap = new HashMap<>();

        logMap.put("sign", sign);
        logMap.put("stepName", stepName);
        logMap.put("exception", exception);
        logMap.put("extra", extra);
        logMap.put("time", new Date().getTime() + "");
        logList.add(logMap);
    }

    public void sendLog(boolean isSuccess) {
        JSONArray jsonArray = new JSONArray();
        Map<String, String> logMap = new HashMap<>();
        logMap.put("sign", "end");
        logMap.put("stepName", logList.get(0).get("stepName"));
        logMap.put("exception", "");
        logMap.put("extra", "");
        logList.add(logMap);
        try {
            if (logList.size() > 0) {
                jsonArray = new JSONArray(new Gson().toJson(logList));
            } else {
                jsonArray = null;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        commitBleReqLog(jsonArray, isSuccess);

    }


    public void commitBleReqLog(JSONArray jsonArray, boolean isSuccess) {
        JSONObject parseJson = new JSONObject();
        try {
            parseJson.put("time", new Date().getTime() + "");
            parseJson.put("opOrder", operateType);
            if (isSuccess) {
                parseJson.put("opResult", "success");
            } else {
                if (logList.size() > 1) {
                    parseJson.put("errType", logList.get(logList.size() - 2).get("stepName"));
                    parseJson.put("errDes", logList.get(logList.size() - 2).get("exception"));
                }
                parseJson.put("opResult", "fail");
            }

            parseJson.put("carBoxType", carBoxType);
            parseJson.put("prjectName", "staff");
            parseJson.put("opUid", MyApplication.getInstance().getUserId());
            parseJson.put("orderid", orderId);


            parseJson.put("staffOid", orderId);
            parseJson.put("mac", carBleName);
            parseJson.put("idc", carBleName);
            parseJson.put("platforms", "android");
            parseJson.put("path", jsonArray);
            parseJson.put("carId", carId);
            parseJson.put("phoneModel", Build.MODEL);
            parseJson.put("logVersion", "0.0.2");

        } catch (JSONException e) {
            e.printStackTrace();
        }

        ThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                /*parse*/
                try {
                    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), parseJson.toString());

                    ParseApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL_PARSE).parseBleLog(requestBody).execute();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
